function DatabaseTests(name) 
{	
	this.base = TestCase;
	this.base(name);
	this.className = "DatabaseTests";
}
DatabaseTests.prototype = new TestCase;

var date1, date2, siteId1, siteId2, duration1, duration2;

DatabaseTests.testing = function () { return [ "file:///home/dafyddj/src/takecontrol/trunk/components/TakeControlService.js" ]; }

DatabaseTests.prototype.setUp = function()
{
	this.database = new Database();
	date1 = new Date(2006, 2, 28).getW3CDate();
	date2 = new Date(2006, 3, 20).getW3CDate();
	duration1 = 33;
	duration2 = 666;
	siteId1 = "www.google.com";
	siteId2 = "www.bbc.co.uk";
}

DatabaseTests.prototype.testCreateStoreWithInMemDatasource = function() {
	assertNotNull(this.database);
	assertTrue(this.database.dsAvailable());
}

DatabaseTests.prototype.testGetSessionDuration_DayExists = function () {
	this.database.updateSessionDuration(siteId1, date1, duration1);
	var duration = this.database.getSessionDuration(siteId1, date1);
	assertEquals(duration1, duration);
}

DatabaseTests.prototype.testGetSessionDuration_DayNonExistant = function () {
	var duration = this.database.getSessionDuration(siteId1, date1);
	assertEquals(0, duration);
}

DatabaseTests.prototype.testGetDailyTotal_DayNonExistant = function () {
	var dailyTotal = this.database.getDailyTotal(date1);
	assertEquals(0, dailyTotal);
	assertEquals("getRowCount()", 0, this._getRowCount("Day"));
}

DatabaseTests.prototype.testGetDailyTotal_DayExists = function () {
	this.database.updateDailyTotal(date1, duration1);
	var dailyTotal = this.database.getDailyTotal(date1);
	assertEquals(duration1, dailyTotal);
	assertEquals("getRowCount()", 1, this._getRowCount("Day"));
}

DatabaseTests.prototype.testUpdateDailyTotal_DayNonExistant = function () {
	this.database.updateDailyTotal(date1, duration1);
	var dailyTotal = this.database.getDailyTotal(date1);
	assertEquals("Inserted duration", duration1, dailyTotal);
	assertEquals("getRowCount()", 1, this._getRowCount("Day"));
}

DatabaseTests.prototype.testUpdateDailyTotal_DayExists = function () {
	this.database.updateDailyTotal(date1, duration1);
	var dailyTotal = this.database.getDailyTotal(date1);
	assertEquals("Initial duration", duration1, dailyTotal);
	this.database.updateDailyTotal(date1, duration2);
	dailyTotal = this.database.getDailyTotal(date1);
	assertEquals("Updated duration", duration2, dailyTotal);
	assertEquals("getRowCount()", 1, this._getRowCount("Day"));
}

DatabaseTests.prototype.testSave = function () {
	this.database.save();
}

DatabaseTests.prototype._getRowCount = function (tableName)
{
	var count = 0;
	var statement = this.database._ds.createStatement
		("SELECT count(rowid) FROM " + tableName);
	try {
		statement.executeStep();
		count = statement.getInt32(0);
	}
	catch(e) { /* do nothing */
		fail(e);
	}
	finally {
		statement.reset();
	}
	return count;
}
